package com.whf.android.jar.tool;

import android.content.ClipData;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import android.content.ClipboardManager;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 复制
 *
 * @author qf
 * @date 2020-03-12
 */
public class CopyButtonUtil {

    private final Context context;
    private final TextView textView;

    public CopyButtonUtil(Context context, TextView textView) {
        this.context = context;
        this.textView = textView;
    }

    public void init() {
        final ClipboardManager myClipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        String text;
        text = textView.getText().toString();

        final ClipData myClip = ClipData.newPlainText("text", text);
        myClipboard.setPrimaryClip(myClip);

        Toast.makeText(context, text + " 已复制", Toast.LENGTH_SHORT).show();
    }

}
